package com.sinosoft.cnmuseum.servlets;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.sinosoft.cnmuseum.dao.MbXtglJsglDao;
import com.sinosoft.cnmuseum.entity.MbXtglJsgl;
import com.sinosoft.cnmuseum.entity.MbXtglYhxx;
import com.sinosoft.cnmuseum.utils.PageUtil;
import com.sinosoft.cnmuseum.utils.Status;
import com.sinosoft.commontools.annotated.rquestMethod;
import com.sinosoft.commontools.annotated.rquestMethod.ReqMode;
import com.sinosoft.commontools.record.Record;

public class MbXtglZjkServlet {
	  private static Logger logger = Logger.getLogger(MbXtglZjkServlet.class);

	@rquestMethod(MethodNote = ReqMode.All)
	public Record getZjk(HttpServletRequest request, HttpServletResponse response, Record data){
		Record result = new Record();
		try {
			MbXtglJsglDao dao = new MbXtglJsglDao();
			  int pagenumber = data.getIntValue(Status.PAGENUMBER);// 获取当前页
			  int pagesize = data.getIntValue(Status.PAGESIZE);// 获取每页多少条数据
			  String countsql = "select count(zdbh) as count from " 
					  + MbXtglJsgl.MB_XTGL_JSGL_TABLE_NAME + " where " 
					  + MbXtglJsgl.JSZT + "=1 and "
			  		  + MbXtglJsgl.SFWZJ + "=1 ";
			  String sql = "select * from " 
					  + MbXtglJsgl.MB_XTGL_JSGL_TABLE_NAME + " where " 
					  + MbXtglJsgl.JSZT + "=1 and "
					  + MbXtglJsgl.SFWZJ + "=1 ";
			  String ordersql = " order by zdbh asc ";
			  String limitsql = " limit ";
			
			  logger.info("CSQL:" + countsql);
			  List<Record> countlist = dao.getBySql(countsql);
			  int total = countlist.get(0).getIntValue("count");
			  PageUtil page = new PageUtil(pagenumber, pagesize, total);
			  limitsql += page.getStart()+","+pagesize;
			  logger.info("SQL:" + sql+ordersql+limitsql);
			  List<Record> list = dao.getBySql(sql+ordersql+limitsql);
			  // 组装数据
			  result.add(Status.ROWS, list);
			  result.add(Status.TOTAL, total);
			  result.add(Status.PAGENUMBER, pagenumber);
			  result.add(Status.PAGESIZE, pagesize);
			  result.add(Status.PAGETOTAL, page.getPagetotal());
			  // 返回接口调用成功标识
			  result.add(Status.SATUS,Status.OK);
		} catch (Exception e) {
			  // 返回接口调用失败标识
			  result.add(Status.SATUS,Status.FAILED);
			  // 返回接口调用失败信息，需要自己添加
			  result.add(Status.MSG,"查询失败！");
		}
		return result;
	}
	
	@rquestMethod(MethodNote = ReqMode.All)
	public Record getZjkUser(HttpServletRequest request, HttpServletResponse response, Record data){
		Record result = new Record();
		try {
			MbXtglJsglDao dao = new MbXtglJsglDao();
			  int pagenumber = data.getIntValue(Status.PAGENUMBER);// 获取当前页
			  int pagesize = data.getIntValue(Status.PAGESIZE);// 获取每页多少条数据
			  String countsql = "select count(zdbh) as count from " 
					  + MbXtglYhxx.MB_XTGL_YHXX_TABLE_NAME + " where " 
			  		  + MbXtglYhxx.SFWZJ + "=1 ";
			  String sql = "select * from " 
					  + MbXtglYhxx.MB_XTGL_YHXX_TABLE_NAME + " where " 
					  + MbXtglYhxx.SFWZJ + "=1 ";
			  String ordersql = " order by zdbh asc ";
			  String limitsql = " limit ";
			
			  logger.info("CSQL:" + countsql);
			  List<Record> countlist = dao.getBySql(countsql);
			  int total = countlist.get(0).getIntValue("count");
			  PageUtil page = new PageUtil(pagenumber, pagesize, total);
			  limitsql += page.getStart()+","+pagesize;
			  logger.info("SQL:" + sql+ordersql+limitsql);
			  List<Record> list = dao.getBySql(sql+ordersql+limitsql);
			  String qxsql = "select fp.rybm,qxgl.* from mb_xtgl_ryjsfp fp "
			      + "left join mb_xtgl_jsgl js on fp.jsbm=js.jsbm "
			      + "left join mb_xtgl_jsqxgx qxgx on js.jsbm=qxgx.jsbm "
			      + "left join mb_xtgl_qxgl qxgl on qxgx.qxbm = qxgl.qxbm "
			      + "where fp.rybm in (select rybm from mb_xtgl_yhxx) "
			      + "and fp.jsbm in (select jsbm from mb_xtgl_jsdhgl where dhbm=2)";

              List<Record> qxlist = dao.getBySql(qxsql);
              Record qxRecord = new Record();
			  for(Record qx:qxlist){
			    String rybm = qx.getStrValue("rybm");
			    
			    if(qxRecord.containsKey(rybm)){
			      List list_ = (List)qxRecord.getValue(rybm);
			      list_.add(qx);
			      qxRecord.add(rybm, list_);
			    }else{
                  List list_ = new ArrayList<>();
                  list_.add(qx);
                  qxRecord.add(rybm, list_);
			    }
			  }
			  for(Record l:list){
			    List list_ = (List)qxRecord.getValue(l.getStrValue("rybm"));
			    l.add("qxlist", list_);
			  }
			  // 组装数据
			  result.add(Status.ROWS, list);
			  result.add(Status.TOTAL, total);
			  result.add(Status.PAGENUMBER, pagenumber);
			  result.add(Status.PAGESIZE, pagesize);
			  result.add(Status.PAGETOTAL, page.getPagetotal());
			  // 返回接口调用成功标识
			  result.add(Status.SATUS,Status.OK);
		} catch (Exception e) {
			  // 返回接口调用失败标识
			  result.add(Status.SATUS,Status.FAILED);
			  // 返回接口调用失败信息，需要自己添加
			  result.add(Status.MSG,"查询失败！");
		}
		return result;
	}
	
	@rquestMethod(MethodNote = ReqMode.All)
	public Record getShrUser(HttpServletRequest request, HttpServletResponse response, Record data){
		Record result = new Record();
		try {
			MbXtglJsglDao dao = new MbXtglJsglDao();
			  int pagenumber = data.getIntValue(Status.PAGENUMBER);// 获取当前页
			  int pagesize = data.getIntValue(Status.PAGESIZE);// 获取每页多少条数据
			  String countsql = "select count(zdbh) as count from " 
					  + MbXtglYhxx.MB_XTGL_YHXX_TABLE_NAME + " where " 
			  		  + MbXtglYhxx.SFWZJ + "=1 ";
			  String sql = "select * from " 
					  + MbXtglYhxx.MB_XTGL_YHXX_TABLE_NAME + " where " 
					  + MbXtglYhxx.SFWZJ + "=1 ";
			  String ordersql = " order by zdbh asc ";
			  String limitsql = " limit ";
			
			  logger.info("CSQL:" + countsql);
			  List<Record> countlist = dao.getBySql(countsql);
			  int total = countlist.get(0).getIntValue("count");
			  PageUtil page = new PageUtil(pagenumber, pagesize, total);
			  limitsql += page.getStart()+","+pagesize;
			  logger.info("SQL:" + sql+ordersql+limitsql);
			  List<Record> list = dao.getBySql(sql+ordersql+limitsql);
			  // 组装数据
			  result.add(Status.ROWS, list);
			  result.add(Status.TOTAL, total);
			  result.add(Status.PAGENUMBER, pagenumber);
			  result.add(Status.PAGESIZE, pagesize);
			  result.add(Status.PAGETOTAL, page.getPagetotal());
			  // 返回接口调用成功标识
			  result.add(Status.SATUS,Status.OK);
		} catch (Exception e) {
			  // 返回接口调用失败标识
			  result.add(Status.SATUS,Status.FAILED);
			  // 返回接口调用失败信息，需要自己添加
			  result.add(Status.MSG,"查询失败！");
		}
		return result;
	}

}
